package com.shanghaionstar.user.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.onstar.cn.common.log.LogManager;
import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.*;
import com.onstar.cn.more.entity.CardHistories;
import com.shanghaionstar.R;

import java.util.List;

/**
 * Created by Mengzhou Gu on 2016/8/3.
 */
public class HistoryAdapter extends com.onstar.cn.framework.base.CommonAdapter<CardHistories> {
    private Context contexts;
    private List<CardHistories> alldata = null;
    private long selectItem = -1;
    private long sign = -1;
    public HistoryAdapter(List<CardHistories> datas, Context context) {
        super(datas, context);
        alldata = datas;
        contexts = context;
    }

    @Override
    public void getView(int position, CommonViewHolder viewHolder) {
        ViewHolder holder = (ViewHolder) viewHolder;
        CardHistories ch = alldata.get(position);
        try {
            String packageName = ch.getPackageInfo().getPackageName();
            if (!CommonUtil.isNull(packageName) && packageName.length() >= 16) {
                packageName = packageName.substring(0, 14) + "...";
            }
            holder.ptitle.setText(packageName);
            holder.price.setText("￥" + ch.getPrice());
            holder.activitedate.setText(ch.getActivateDate().substring(0, 10));
            holder.carnumber.setText(ch.getCardNo());
            holder.vin.setText(ch.getVehicleId());
            if (ch.getPackageInfo().getPackageType().equalsIgnoreCase("data")) {
                holder.tv_star_date.setVisibility(View.INVISIBLE);
                holder.stardate.setVisibility(View.INVISIBLE);
                holder.tv_end_date.setText(R.string.effect_date_value);
                holder.enddate.setText(ch.getPackageInfo().getDuration() + contexts.getString(R.string.ppc_day));
            } else {
                LogManager.getInstance().d(ch.getPackageInfo().getStartDate().substring(0, 10));
                holder.tv_star_date.setVisibility(View.VISIBLE);
                holder.stardate.setVisibility(View.VISIBLE);
                holder.stardate.setText(ch.getPackageInfo().getStartDate().substring(0, 10));
                holder.tv_end_date.setText(R.string.end_date_value);
                holder.enddate.setText(ch.getPackageInfo().getEndDate().substring(0, 10));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public int layoutId() {
        return R.layout.active_history_item;
    }

    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    public void setSelectItem(long arg) {
        this.selectItem = arg;
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById(value = R.id.layout_detail)
        public RelativeLayout layout;
        @FindViewById
        public ImageView      iv_show_detail;
        @FindViewById(value = R.id.package_name)
        public TextView       ptitle;
        @FindViewById(value = R.id.tv_package_name_value)
        public TextView       price;
        @FindViewById(value = R.id.tv_activite_date_value)
        public TextView       activitedate;
        @FindViewById(value = R.id.tv_prepay_number_value)
        public TextView       carnumber;
        @FindViewById(value = R.id.tv_car_recognition_value)
        public TextView       vin;
        @FindViewById(value = R.id.tv_history_start_date)
        public TextView       stardate;
        @FindViewById(value = R.id.tv_history_end_date)
        public TextView       enddate;
        @FindViewById
        public TextView       tv_star_date;
        @FindViewById
        public TextView tv_end_date;
    }
}
